package priv.conceit.sc.hoxton.common.web.feign;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * sc-hoxton feign对应的拦截器
 *
 *
 * @author Conceit
 * @since 1.0.0, 2020/11/12
 */
@Configuration
public class TokenInterceptor implements RequestInterceptor {

	/**
	 * apply 装载token请求头
	 *
	 * @param requestTemplate
	 * @author conceit
	 * @date 2020/11/12 16:10
	 */
	@Override
	public void apply(RequestTemplate requestTemplate) {
		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = attributes.getRequest();
		//获取token
		String requestToken = request.getHeader("token");
		requestTemplate.header("token", requestToken);
	}
}
